package com.healthcloud.controller;

import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.bean.TestDateEntity;
import cn.jpush.api.push.PushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.healthcloud.entity.PpPatientBaseEntity;
import com.healthcloud.service.*;
import com.healthcloud.utils.IdcardInfoExtractorUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 消息推送
 * Created by yucl on 2017/10/25.
 */
@ApiIgnore
@Controller
@RequestMapping("/jpush")
public class publishController {

    protected static final String APP_KEY = "5c09bc200bf129eaf5551dc3";
    protected static final String MASTER_SECRET = "c350b0e11e0f49e858617308";

    private final static Logger log = LoggerFactory.getLogger(publishController.class);

    @Autowired
    private BusInstrumentService busInstrumentService;

    @Autowired
    private JpushInfoService jpushInfoService;

    @Autowired
    private SavingSampleInfo2Service saveavingSampleInfo;

    @RequestMapping(value = "/send",method = RequestMethod.POST)
    public PushResult send(@RequestBody TestDateEntity testDateEntity) throws Exception{

        log.info("start jpush");

        PushClient _client = new PushClient(MASTER_SECRET, APP_KEY);

        String alert = "SMART Ⅳ有一条新检测结果！设备SN：["+testDateEntity.getMKMedicalinstrumentId()+"]。查看详情＞＞。";

        if(testDateEntity ==null || StringUtils.isBlank(testDateEntity.getMKMedicalinstrumentId()) ){
            return null;
        }

        List<String> openIDs = busInstrumentService.getOpenID(testDateEntity.getMKMedicalinstrumentId());

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(openIDs))
                .setNotification(Notification.alert(alert))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();


        //设备平台中的数据
//        saveavingSampleInfo.saveJpushInfo(testDateEntity);
//        return null;

        PushResult result = new PushResult();
        try {

            //保存到病人表
            jpushInfoService.savePatient(testDateEntity);

            //保存到推送表
            jpushInfoService.saveJpushInfoEntity(testDateEntity,testDateEntity.getMKMedicalinstrumentId());

            //设备平台中的数据
            saveavingSampleInfo.saveJpushInfo(testDateEntity);


            result = _client.sendPush(payload);
            return result;

        } catch (APIRequestException e) {
            e.printStackTrace();
            log.error(e.getErrorMessage());

            result = new PushResult();
            result.statusCode = 500;
            result.setMessage(e.getMessage());
            return result;
        }
    }
}
